<?php
/**
 * Created by PhpStorm.
 * User: phperstar
 * Date: 2020/6/17
 * Time: 3:35 PM
 */

namespace Util\CloudPrint;

use JinDouYun\Controller\Common\Logger;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\ErrorCode;


class Gainshca
{
    /**
     * 商户编码
     */
    private $memberCode;

    /**
     * 终端编号
     */
    private $deviceID;

    /**
     * API密钥
     */
    private $apiKey;


    /**
     * 微信小程序接口主域名
     * @var string $apiUrl
     */
    private $apiUrl = 'http://api.poscom.cn/apisc/';

    public function __construct($memberCode='', $deviceID='', $apiKey = '')
    {
        $this->memberCode = $memberCode;
        $this->deviceID   = $deviceID;
        $this->apiKey     = $apiKey;
    }

    /**
     * 添加打印机设备
     */
    public function adddev()
    {
        $postData = [
            'reqTime' => msectime(),
            'securityCode' => md5($this->memberCode.msectime().$this->apiKey.$this->deviceID),
            'memberCode' => $this->memberCode,
            'deviceID'   => $this->deviceID,
            'devName'    => '千鸟云商',
        ];

        $apiUrl = $this->apiUrl.'adddev';
        $reponse =  request($apiUrl, $postData);

        if($reponse['httpcode'] != 200){
            return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
        }else{
            $reponseData = json_decode($reponse['content'], true);
            if( isset($reponseData['code']) && $reponseData['code'] != 1){
                return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
            }
            return ResultWrapper::success(['devID'=>$reponseData['devID']]);
        }
    }

    /**
     * 发送信息到打印机
     * @param $params
     * @return ResultWrapper
     */
    public function sendMsg($params)
    {
        $postData = [
            'reqTime' => msectime(),
            'securityCode' => md5($this->memberCode.$this->deviceID.msectime().$this->apiKey),
            'memberCode' => $this->memberCode,
            'deviceID'   => $this->deviceID,
            'mode' => 2,
            'msgDetail' => $params
        ];
        $apiUrl = $this->apiUrl.'sendMsg';
        $reponse =  request($apiUrl, $postData);

        if($reponse['httpcode'] != 200){
            return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
        }else{
            $reponseData = json_decode($reponse['content'], true);
            if( isset($reponseData['code']) && $reponseData['code'] != 0){
                return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
            }
            return ResultWrapper::success(['devID'=>$reponseData['devID']]);
        }
    }

    /**
     * 查询打印机状态
     * @param $deviceID
     * @param $selectAll
     * @return ResultWrapper
     */
    public function getStatus($deviceID, $selectAll)
    {
        $postData = [
            'reqTime' => msectime(),
            'securityCode' => md5($this->memberCode.msectime().$this->apiKey),
            'memberCode' => $this->memberCode,
            'deviceID'   => $this->deviceID,
        ];
        if($deviceID) $postData['deviceID'] = $deviceID;
        if($selectAll) unset($postData['deviceID']);

        $apiUrl = $this->apiUrl.'getStatus';
        $reponse =  request($apiUrl, $postData);

        if($reponse['httpcode'] != 200){
            return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
        }else{
            $reponseData = json_decode($reponse['content'], true);
            if( isset($reponseData['code']) && $reponseData['code'] != 1){
                return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
            }
            return ResultWrapper::success($reponseData['statusList']);
        }
    }
    /**
     * 查询打印机状态
     * @param $deviceID
     * @param $selectAll
     * @return ResultWrapper
     */
    public function setVolume($num)
    {
        $postData = [
            'reqTime' => msectime(),
            'securityCode' => md5($this->memberCode.msectime().$this->apiKey.$this->deviceID),
            'memberCode' => $this->memberCode,
            'deviceID'   => $this->deviceID,
            'volume' => $num
        ];

        $apiUrl = $this->apiUrl.'sendVolume';
        $reponse =  request($apiUrl, $postData);

        if($reponse['httpcode'] != 200){
            return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
        }else{
            $reponseData = json_decode($reponse['content'], true);
            if( isset($reponseData['code']) && $reponseData['code'] != 1){
                return ResultWrapper::fail($reponseData['msg'], $reponseData['code']);
            }
            return ResultWrapper::success(['devID'=>$reponseData['devID']]);
        }
    }

}